package FirstProject;

import day13.User;
import sun.net.idn.Punycode;

import javax.swing.text.html.CSS;
import java.security.PublicKey;
import java.text.BreakIterator;
import java.util.*;

public class TestUntil {
    static Scanner sc = new Scanner(System.in);
    static CardUtil cu = new CardUtil();
//    static Map<String,MobileCard> cards= new HashMap<>();
    //static Map<String, List<ConsumeInfo>> consumeInfos=new HashMap<>();
    public static void main(String[] args) {
        testUser();
        one();
    }
    public static void testUser(){
        MobileCard testCd=new MobileCard();
        testCd.setCardNumber("13900000000");
        testCd.setUserName("lili");
        testCd.setPassWord("123456");
        testCd.setMoney(100);
        testCd.setSerPackage(new TalkPackage(98));
        cu.cards.put(testCd.getCardNumber(),testCd);
        cu.consumeInfos.put(testCd.getCardNumber(), new ArrayList<>());
    }
    public static void one() {
        MobileCard currentUser=null;
        do{
            System.out.println("o(=•ェ•=)o欢迎使用喵呜5G移动业务大厅o(=•ェ•=)o");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.使用喵呜");
            System.out.println("4.话费充值");
            System.out.println("5.资费说明");
            System.out.println("6.退出系统");
            int choice = sc.nextInt();
            switch (choice) {
                case 1: currentUser= login();
                        if(currentUser!=null){
                            two(currentUser);
                        } break;
                case 2: register();break;
                case 3:
                    System.out.println("请输入手机号:");
                    String number=sc.next();
                    cu.useSoso(number);break;
                case 4:
                    System.out.println("请输入您要充值的金额:");
                    double n=sc.nextDouble();
                    cu.chargeMoney(currentUser.getCardNumber(), n);
                    System.out.println("您已成功充值"+n+"当前余额为");
                    //剩余
                     break;
                case 5: cu.showDescription();break;
                case 6: return;
        }}while (true);
    }
    public static MobileCard login () {//返回登录的用户
        System.out.println("o(=•ェ•=)m 欢迎登录 o(=•ェ•=)m");
        System.out.println("请输入用户名:");
        String name=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        for(MobileCard card:cu.cards.values()){
            if (name.equals(card.getUserName()) &&
                    password.equals(card.getPassWord())){
                System.out.println("成功登录,欢迎您~");
                return card;
            }
        }
        System.out.println("sorry~您的账号或密码错误(●'◡'●)");
        return null;
    }
    public static void register (){
        String[] phones=randPhones();
        System.out.println("o(=•ェ•=) m欢迎您注册 o(=•ェ•=)m");
        for(int i=0;i< phones.length;i++){
            System.out.print(i+1+" "+ phones[i]+" ");
            if((i+1)%3==0) System.out.println();
        }
        System.out.println("🐕请输入您心仪的手机号(输入1-9的序号)🐕:");
        int n=sc.nextInt();
        String phone=phones[n-1];
        MobileCard newCard = new MobileCard();
        newCard.setCardNumber(phone);
        System.out.println("请输入姓名:");
        newCard.setUserName(sc.next());
        System.out.println("请输入密码:");
        newCard.setPassWord(sc.next());
        System.out.println("🐱请选择您心仪的套餐🐱(输入序号)");
        System.out.println("1.话痨套餐  2.网虫套餐  3.超人套餐");
        int n1=sc.nextInt();
        switch (n1){
            case 1:newCard.setSerPackage(new  TalkPackage(58)); break;
            case 2:newCard.setSerPackage(new NetPackage(68)); break;
            case 3:newCard.setSerPackage(new SuperPackage(78)); break;
        }
        cu.cards.put(phone,newCard);
        //System.out.println("请输入预存的话费金额:");
        double moneyInput=0;
        do {
            System.out.print("请输入预存的话费金额(至少" + cu.cards.get(phone).getSerPackage().price + "元)：");
            try {
                moneyInput=sc.nextDouble();
                if (moneyInput < cu.cards.get(phone).getSerPackage().price) {
                    System.out.println("您充值的钱太少啦！至少需要" + cu.cards.get(phone).getSerPackage().price + "元");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的金额！");
                moneyInput = 0;
            }
        } while (moneyInput < cu.cards.get(phone).getSerPackage().price);
    }
    public static String[] randPhones() {
        Random r=new Random();
        String[] phones=new String[9];
        for(int i=0;i<phones.length;i++){
            StringBuilder sb=new StringBuilder("139");
            for(int j=0;j<8;j++){
                sb.append(r.nextInt(10));//0-9
            }
            phones[i]=sb.toString();
        }
        return phones;
    }
    public static void two(MobileCard currentUser){//登录成功后进入二级菜单
        System.out.println("o(=•ェ•=)m 喵呜5G移动用户菜单 o(=•ェ•=)m");
        ServicePackage sp= currentUser.getSerPackage();
        boolean b=true;
        while (b) {
            System.out.println("1.本月账单查询");
            System.out.println("2.套餐余量查询");
            System.out.println("3.打印消费详单");
            System.out.println("4.套餐变更");
            System.out.println("5.办理退网");
            int m = sc.nextInt();
            //其他键返回上一级没写
            System.out.println("请选择(输入1-5序号)");
            switch (m) {
                case 1:
                    cu.showAmountDetail(currentUser.cardNumber);
                    break;
                case 2:
                    cu.showRemainDetail(currentUser.cardNumber);
                    break;
                case 3:
                    cu.printConsumeInfo(currentUser.cardNumber);
                    break;
                case 4:
                    //cu.changingPack(currentUser.userName);
                    break;
                case 5:
                    b=false;
                    return;
            }
        }
    }

}